package controler;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import castleContent.User;
import dataBase.DataBase;
import dataBase.UpdateDBTables;

@WebServlet("/UpgradeServlet")
public class UpgradeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		String warriorUpgradeType = request.getParameter("warriorUpgradeType");
		User user = (User) session.getAttribute("Object");

		try {
			barbarianUpgrade(response, warriorUpgradeType, user);
			archerUpgrade(response, warriorUpgradeType, user);
			wizardUpgrade(response, warriorUpgradeType, user);
			printResourceAndArmyCapacity(response, user);
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		}

	}

	// Upgrade Barbarian
	private void barbarianUpgrade(HttpServletResponse response,
			String warriorUpgradeType, User user) throws IOException,
			ClassNotFoundException, SQLException {
		if (isThereEnoughGold(user)) {
			if (warriorUpgradeType.equals("BarbarianAtt")) {
				if (user.castle.getBarracks().getBarbarianADLevel() < 5) {
					user.castle.barbarianAdLevelUp();
					UpdateDBTables.updateGoldWhenUpgradeBarbarianAtt(
							DataBase.getDbCon(), user.getName());
					UpdateDBTables.updateBarbarianADLevel(DataBase.getDbCon(),
							user.getName());
				}
			} else if (warriorUpgradeType.equals("BarbarianHp")) {
				if (user.castle.getBarracks().getBarbarianHPLevel() < 5) {
					user.castle.barbarianHpLevelUp();
					UpdateDBTables.updateGoldWhenUpgradeBarbarianHp(
							DataBase.getDbCon(), user.getName());
					UpdateDBTables.updateBarbarianHPLevel(DataBase.getDbCon(),
							user.getName());
				}
			}
		}
	}

	// Upgrade Archer
	private void archerUpgrade(HttpServletResponse response,
			String warriorUpgradeType, User user) throws IOException,
			ClassNotFoundException, SQLException {
		if (isThereEnoughGold(user)) {
			if (warriorUpgradeType.equals("ArcherAtt")) {
				if (user.castle.getBarracks().getArcherADLevel() < 5) {
					user.castle.archerAdLevelUp();
					UpdateDBTables.updateGoldWhenUpgradeArcherAtt(
							DataBase.getDbCon(), user.getName());
					UpdateDBTables.updateArcherADLevel(DataBase.getDbCon(),
							user.getName());
				}
			} else if (warriorUpgradeType.equals("ArcherHp")) {
				if (user.castle.getBarracks().getArcherHPLevel() < 5) {
					user.castle.archerHpLevelUp();
					UpdateDBTables.updateGoldWhenUpgradeArcherHp(
							DataBase.getDbCon(), user.getName());
					UpdateDBTables.updateArcherHPLevel(DataBase.getDbCon(),
							user.getName());
				}
			}
		}
	}

	// Upgrade Wizard
	private void wizardUpgrade(HttpServletResponse response,
			String warriorUpgradeType, User user) throws IOException,
			ClassNotFoundException, SQLException {
		if (isThereEnoughGold(user)) {
			if (warriorUpgradeType.equals("WizardAtt")) {
				if (user.castle.getBarracks().getWizzardADLevel() < 5) {
					user.castle.wizzardAdLevelUp();
					UpdateDBTables.updateGoldWhenUpgradeWizardHp(
							DataBase.getDbCon(), user.getName());
					UpdateDBTables.updateWizardADLevel(DataBase.getDbCon(),
							user.getName());
				}
			} else if (warriorUpgradeType.equals("WizardHp")) {
				if (user.castle.getBarracks().getWizzardHPLevel() < 5) {
					user.castle.wizzardHpLevelUp();
					UpdateDBTables.updateGoldWhenUpgradeWizardAtt(
							DataBase.getDbCon(), user.getName());
					UpdateDBTables.updateWizardHPLevel(DataBase.getDbCon(),
							user.getName());
				}
			}
		}
	}

	// print Resources
	private void printResourceAndArmyCapacity(HttpServletResponse response,
			User user) throws IOException {
		response.getWriter().print(
				"Food : " + user.castle.getFoodFarm().getAvailableResource()
						+ "	Gold : "
						+ user.castle.getGoldMine().getAvailableResource()
						+ "<br>Army : "
						+ user.castle.getBarracks().getPresentArmy() + " / "
						+ user.castle.getBarracks().getArmyCapacity() + "<br>"
						+ "Barbarians : "
						+ user.castle.getBarracks().getBarbarians()
						+ " Archers : "
						+ user.castle.getBarracks().getArchers()
						+ " Wizards : "
						+ user.castle.getBarracks().getWizzards());
	}

	private boolean isThereEnoughGold(User user) {
		return user.castle.getGoldMine().getAvailableResource() >= 500;
	}
}